package com.lcy.controller;

import com.lcy.service.LoginService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @RequestMapping("/admin/login")
    public String adminLogin(){
        return "login";
    }

    @Autowired
    LoginService loginService;

    @RequestMapping("/login.do")
    public String login(HttpServletRequest req, HttpServletResponse resp){
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");
        // LoginService loginService = new LoginServiceImpl();
        boolean result = loginService.login(userCode,userPassword);
        if(result){
            HttpSession httpSession = req.getSession();
            httpSession.setAttribute("userName",userCode);
            return "jsp/frame";
        }else{
            HttpSession httpSession = req.getSession();
            httpSession.setAttribute("error","用户名或密码错误");
            return "login";
        }
    }

    @RequestMapping("/jsp/logout.do")
    public String loginOut(HttpSession httpSession){
        httpSession.invalidate();
        return "login";
    }
}
